# import requests
# from lxml import etree
# import csv
# headers = {
#     'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.96 Safari/537.36'
# }
#
# result = {
#     'title': [],  # 小说名
#     'author': [],  # 作者
#     'type': [],  # 类型
#     'progress': [],  # 状态
#     'juqing': [],  # 剧情
#     'now': [],  # 最新更新章节
#     'time': []  # 最新更新时间
# }
# for i in range(1, 6):
#     url = f'https://www.qidian.com/rank/hotsales?style=1&page={i}'
#     response = requests.get(url, headers=headers)
#     abc = response.status_code
#     print(abc)
#     html = etree.HTML(response.text)
#     # 书籍名称
#     titles = html.xpath('//div[@class="book-mid-info"]/h4/a/text()')
#     result['title'].extend(titles)
#     # 作者
#     authors = html.xpath('//div[@class="book-mid-info"]/p[@class="author"]/a[1]/text()')
#     result['author'].extend(authors)
#     # 类别
#     types = html.xpath('//div[@class="book-mid-info"]/p[@class="author"]/a[2]/text()')
#     result['type'].extend(types)
#     # 状态
#     progresses = html.xpath('//div[@class="book-mid-info"]/p[@class="author"]/span/text()')
#     result['progress'].extend(progresses)
#     # 剧情介绍
#     juqings = html.xpath('//div[@class="book-mid-info"]/p[@class="intro"]/text()')
#     result['juqing'].extend(juqings)
#     # 最新更新章节
#     nows = html.xpath('//div[@class="book-mid-info"]/p[@class="update"]/a/text()')
#     result['now'].extend(nows)
#     # 最新更新时间
#     times = html.xpath('//div[@class="book-mid-info"]/p[@class="update"]/span/text()')
#     result['time'].extend(times)
# with open(f'D:\kbooks.csv', 'a', newline='', encoding='utf-8') as csvfile:
#     fieldnames = ['小说名', '作者', '类型', '状态', '剧情介绍', '最新更新章节', '最新更新时间']
#     writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
#     writer.writeheader()
#     for j in range(len(result['title'])):
#         writer.writerow({
#             'title': result['title'][j],
#             'author': result['author'][j],
#             'type': result['type'][j],
#             'progress': result['progress'][j],
#             'juqing': result['juqing'][j],
#             'now': result['now'][j],
#             'time': result['time'][j]
#         })
# print("爬虫完成!")